//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CPICoupon.h"
using namespace Cephei::QL::Cashflows;
#include <gen/QL/Indexes/ZeroInflationIndex.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/InflationIndex.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Cashflows/InflationCouponPricer.h>
#include <gen/QL/Cashflows/InflationCoupon.h>
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Cashflows::CCPICoupon::CCPICoupon (Double baseCPI, DateTime paymentDate, Double nominal, DateTime startDate, DateTime endDate, UInt32 fixingDays, Cephei::QL::Indexes::IZeroInflationIndex^ index, Cephei::QL::Times::IPeriod^ observationLag, QL::Cashflows::CPI::InterpolationTypeEnum observationInterpolation, Cephei::QL::Times::IDayCounter^ dayCounter, Double fixedRate, Microsoft::FSharp::Core::FSharpOption<Double>^ spread, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd, Cephei::QL::Cashflows::IInflationCouponPricer^ QL_Pricer) : CInflationCoupon(CCPICoupon::typeid)
{
    CZeroInflationIndex^ _Cindex;
    CPeriod^ _CobservationLag;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phCPICoupon = NULL;
#endif
        QuantLib::Real _baseCPI = (QuantLib::Real)ValueHelper::Convert (baseCPI); //d
        QuantLib::Date _paymentDate = (QuantLib::Date)ValueHelper::Convert (paymentDate); //d
        QuantLib::Real _nominal = (QuantLib::Real)ValueHelper::Convert (nominal); //d
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //d
        QuantLib::Date _endDate = (QuantLib::Date)ValueHelper::Convert (endDate); //d
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        _Cindex = safe_cast<CZeroInflationIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::ZeroInflationIndex>& _index = static_cast<boost::shared_ptr<QuantLib::ZeroInflationIndex>&> (_Cindex->GetShared ()); 
        _CobservationLag = safe_cast<CPeriod^> (observationLag);
        _CobservationLag->Lock();
        QuantLib::Period& _observationLag = static_cast<QuantLib::Period&> (_CobservationLag->GetReference ()); 
        QuantLib::CPI::InterpolationType _observationInterpolation = (QuantLib::CPI::InterpolationType)observationInterpolation ;
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Real _fixedRate = (QuantLib::Real)ValueHelper::Convert (fixedRate); //d
        QuantLib::Spread _spread = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (spread) ? (QuantLib::Spread)ValueHelper::Convert (spread->Value) : 0.0); //4
        QuantLib::Date _refPeriodStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodStart) ? (QuantLib::Date)ValueHelper::Convert (refPeriodStart->Value) : QuantLib::Date()); //4
        QuantLib::Date _refPeriodEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refPeriodEnd) ? (QuantLib::Date)ValueHelper::Convert (refPeriodEnd->Value) : QuantLib::Date()); //4
        _ppCPICoupon = new boost::shared_ptr<QuantLib::CPICoupon> (new QuantLib::CPICoupon ( _baseCPI,  _paymentDate,  _nominal,  _startDate,  _endDate,  _fixingDays,  _index,  _observationLag,  _observationInterpolation,  _dayCounter,  _fixedRate,  _spread,  _refPeriodStart,  _refPeriodEnd ));
        CInflationCouponPricer^ _CQL_Pricer = safe_cast<CInflationCouponPricer^> (QL_Pricer);
        boost::shared_ptr<QuantLib::InflationCouponPricer>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::InflationCouponPricer>&> (_CQL_Pricer->GetShared ());
        (*_ppCPICoupon)->setPricer (_QL_Pricer);
        SetInflationCoupon (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppCPICoupon));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CobservationLag != nullptr) _CobservationLag->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Cashflows::CCPICoupon::CCPICoupon (boost::shared_ptr<QuantLib::CPICoupon>& childNative, Object^ owner) : CInflationCoupon(CCPICoupon::typeid)
{
#ifdef HANDLE
	_phCPICoupon = NULL;
#endif
	_ppCPICoupon = &childNative;
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppCPICoupon));
}
Cephei::QL::Cashflows::CCPICoupon::CCPICoupon (QuantLib::CPICoupon& childNative, Object^ owner) : CInflationCoupon(CCPICoupon::typeid)
{
#ifdef HANDLE
	_phCPICoupon = NULL;
#endif
	_ppCPICoupon = new boost::shared_ptr<QuantLib::CPICoupon> (&childNative);
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppCPICoupon));
    _CPICouponOwner = owner;
    _InflationCouponOwner = owner;
}

Cephei::QL::Cashflows::CCPICoupon::CCPICoupon (CCPICoupon^ copy) : CInflationCoupon(CCPICoupon::typeid)
{
#ifdef HANDLE
	_phCPICoupon = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCPICoupon = new boost::shared_ptr<QuantLib::CPICoupon> (copy->GetShared());
        _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppCPICoupon));
    }
}
Cephei::QL::Cashflows::CCPICoupon::CCPICoupon (PLATFORM::Type^ t) : CInflationCoupon(CCPICoupon::typeid)
{
#ifdef HANDLE
	_phCPICoupon = NULL;
#endif
	if (!t->IsSubclassOf(CCPICoupon::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Cashflows::CCPICoupon::CCPICoupon (QuantLib::Handle<QuantLib::CPICoupon>& childNative, Object^ owner)  : CInflationCoupon(CCPICoupon::typeid)
{
	_phCPICoupon = &childNative;
	_ppCPICoupon = &static_cast<boost::shared_ptr<QuantLib::CPICoupon>>(childNative.currentLink());
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppCPICoupon));
    _CPICouponOwner = owner;
}
Cephei::QL::Cashflows::CCPICoupon::CCPICoupon (QuantLib::Handle<QuantLib::CPICoupon> childNative)  : CInflationCoupon(CCPICoupon::typeid)
{
	_phCPICoupon = &childNative;
	_ppCPICoupon = &static_cast<boost::shared_ptr<QuantLib::CPICoupon>>(childNative.currentLink());
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppCPICoupon));
}
#endif
#ifdef STRUCT
Cephei::QL::Cashflows::CCPICoupon::CCPICoupon (QuantLib::CPICoupon childNative)  : CInflationCoupon(CCPICoupon::typeid)
{
#ifdef HANDLE
	_phCPICoupon = NULL;
#endif
	_ppCPICoupon = new boost::shared_ptr<QuantLib::CPICoupon> (new QuantLib::CPICoupon (childNative));
    _ppInflationCoupon = new boost::shared_ptr<QuantLib::InflationCoupon> (boost::dynamic_pointer_cast<QuantLib::InflationCoupon> (*_ppCPICoupon));
}
#endif

Cephei::QL::Cashflows::CCPICoupon::~CCPICoupon ()
{
    if (_ppCPICoupon != NULL)
    {
	    delete _ppCPICoupon;
        _ppCPICoupon = NULL;
    }
}
Cephei::QL::Cashflows::CCPICoupon::!CCPICoupon ()
{
    if (_ppCPICoupon != NULL)
    {
	    delete _ppCPICoupon;
    }
}
QuantLib::CPICoupon& Cephei::QL::Cashflows::CCPICoupon::GetReference ()
{
    if (_ppCPICoupon == NULL) throw REFNEW NativeNullException ();
	return **_ppCPICoupon;
}
boost::shared_ptr<QuantLib::CPICoupon>& Cephei::QL::Cashflows::CCPICoupon::GetShared ()
{
    if (_ppCPICoupon == NULL) throw REFNEW NativeNullException ();
	return *_ppCPICoupon;
}
QuantLib::CPICoupon* Cephei::QL::Cashflows::CCPICoupon::GetPointer ()
{
    if (_ppCPICoupon == NULL) throw REFNEW NativeNullException ();
	return &**_ppCPICoupon;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CPICoupon>& Cephei::QL::Cashflows::CCPICoupon::GetHandle ()
{
	if (_phCPICoupon == NULL)
	{
		_phCPICoupon = new Handle<QuantLib::CPICoupon> (*_ppCPICoupon);
	}
	return *_phCPICoupon;
}
#endif
bool Cephei::QL::Cashflows::CCPICoupon::HasNative () 
{
	return (_ppCPICoupon != NULL);
}

Double Cephei::QL::Cashflows::CCPICoupon::AdjustedFixing::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCPICoupon)->adjustedFixing ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCPICoupon::BaseCPI::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCPICoupon)->baseCPI ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::IZeroInflationIndex^ Cephei::QL::Cashflows::CCPICoupon::CpiIndex::get ()
{
    try
    {
    	boost::shared_ptr<QuantLib::ZeroInflationIndex> _rv = (boost::shared_ptr<QuantLib::ZeroInflationIndex>)(*_ppCPICoupon)->cpiIndex ( );   
        Cephei::QL::Indexes::CZeroInflationIndex^ _nrv = REFNEW Cephei::QL::Indexes::CZeroInflationIndex (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCPICoupon::FixedRate::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCPICoupon)->fixedRate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCPICoupon::IndexFixing::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCPICoupon)->indexFixing ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCPICoupon::IndexObservation (DateTime onDate)
{
    try
    {
        QuantLib::Date _onDate = (QuantLib::Date)ValueHelper::Convert (onDate); //a
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppCPICoupon)->indexObservation ( _onDate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Cashflows::CPI::InterpolationTypeEnum Cephei::QL::Cashflows::CCPICoupon::ObservationInterpolation::get ()
{
    try
    {
    	QuantLib::CPI::InterpolationType _rv = (QuantLib::CPI::InterpolationType)(*_ppCPICoupon)->observationInterpolation ( );   
        QL::Cashflows::CPI::InterpolationTypeEnum _nrv = (QL::Cashflows::CPI::InterpolationTypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Cashflows::CCPICoupon::Spread::get ()
{
    try
    {
    	QuantLib::Spread _rv = (QuantLib::Spread)(*_ppCPICoupon)->spread ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Cashflows::ICPICoupon^ Cephei::QL::Cashflows::CCPICoupon_Factory::Create (Double baseCPI, DateTime paymentDate, Double nominal, DateTime startDate, DateTime endDate, UInt32 fixingDays, Cephei::QL::Indexes::IZeroInflationIndex^ index, Cephei::QL::Times::IPeriod^ observationLag, QL::Cashflows::CPI::InterpolationTypeEnum observationInterpolation, Cephei::QL::Times::IDayCounter^ dayCounter, Double fixedRate, Microsoft::FSharp::Core::FSharpOption<Double>^ spread, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refPeriodEnd, Cephei::QL::Cashflows::IInflationCouponPricer^ QL_Pricer)
{
    return REFNEW CCPICoupon ( baseCPI,  paymentDate,  nominal,  startDate,  endDate,  fixingDays,  index,  observationLag,  observationInterpolation,  dayCounter,  fixedRate,  spread,  refPeriodStart,  refPeriodEnd,  QL_Pricer);
}
